package com.lill.system.serivce.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lill.common.component.util.StringUtils;
import com.lill.system.constant.SystemConstant;
import com.lill.system.dao.ISysMenuDao;
import com.lill.system.dao.ISysUserDao;
import com.lill.system.model.SysMenuEntity;
import com.lill.system.model.SysUserEntity;
import com.lill.system.service.ISysMenuService;


@Service("sysMenuService")
public class SysMenuServiceImpl implements ISysMenuService {
    @Autowired
    private ISysMenuDao sysMenuDao;
    @Autowired
    private ISysUserDao sysUserDao;

    public List<SysMenuEntity> queryListParentId(String parentId, List<String> menuIdList, String domainId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("parentId", parentId);
        if (StringUtils.isNotEmpty(domainId)) {
            map.put("domainId", domainId);
        }
        List<SysMenuEntity> menuList = sysMenuDao.queryListParentId(map);
        if (menuIdList == null) {
            return menuList;
        }

        List<SysMenuEntity> userMenuList = new ArrayList<SysMenuEntity>();
        for (SysMenuEntity menu : menuList) {
            if (menuIdList.contains(menu.getId())) {
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    public List<SysMenuEntity> queryNotButtonList() {
        return sysMenuDao.queryNotButtonList();
    }

    public List<SysMenuEntity> getUserMenuList(String userId, String domainId) {
        //系统管理员，拥有最高权限
        if (SystemConstant.SUPER_ADMIN.equals(userId)) {
            return getAllMenuList(null, domainId);
        }

        //用户菜单列表
        List<String> menuIdList = sysUserDao.queryAllMenuId(userId);
        return getAllMenuList(menuIdList, domainId);
    }

    public SysMenuEntity queryObject(String menuId) {
        return sysMenuDao.queryObject(menuId);
    }

    public SysMenuEntity queryUrlObject(String url) {
        return sysMenuDao.queryUrlObject(url);
    }

    public List<SysMenuEntity> queryList(Map<String, Object> map) {
        return sysMenuDao.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return sysMenuDao.queryTotal(map);
    }

    public int save(SysMenuEntity menu,SysUserEntity user) {
        String parentId = menu.getParentId();
        String maxId = sysMenuDao.queryMaxIdByParentId(parentId);

        menu.setId(StringUtils.addOne(parentId, maxId));
        menu.setCreateUser(user.getId());
        menu.setCreateTime(new Date());
        return sysMenuDao.save(menu);
    }

    public int update(SysMenuEntity menu,SysUserEntity user) {
        menu.setUpdateUser(user.getId());
        menu.setUpdateTime(new Date());
        return sysMenuDao.update(menu);
    }

    @Transactional
    public int deleteBatch(String[] menuIds) {
        return sysMenuDao.deleteBatch(menuIds);
    }

    public int deleteUrl(String url) {
        return sysMenuDao.deleteUrl(url);
    }
    public List<SysMenuEntity> queryUserList(String userId) {
        return sysMenuDao.queryUserList(userId);
    }

    /**
     * 获取所有菜单列表
     */
    private List<SysMenuEntity> getAllMenuList(List<String> menuIdList, String domainId) {
        //查询根菜单列表
        List<SysMenuEntity> menuList = queryListParentId("0", menuIdList, domainId);
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList, domainId);

        return menuList;
    }

    /**
     * 递归
     */
    private List<SysMenuEntity> getMenuTreeList(List<SysMenuEntity> menuList, List<String> menuIdList, String domainId) {
        List<SysMenuEntity> subMenuList = new ArrayList<SysMenuEntity>();

        for (SysMenuEntity entity : menuList) {
            if (entity.getType() == 0 /*MenuType.CATALOG.getValue()*/) {//目录
                entity.setList(getMenuTreeList(queryListParentId(entity.getId(), menuIdList, domainId), menuIdList, domainId));
            }
            subMenuList.add(entity);
        }

        return subMenuList;
    }

	@Override
	public int deleteLogicByMap(Map<String, Object> params) {
		int result=sysMenuDao.deleteLogicByMap(params);
		deleteMenuChild(params);
		
		return result;
	}

	private void deleteMenuChild(Map<String, Object> params) {
		List<String> menuIds=sysMenuDao.queryMenusByChildIds(params);
		if(null!=menuIds&&menuIds.size()>0){
			params.put("ids", menuIds);
			sysMenuDao.deleteLogicByMap(params);
			deleteMenuChild(params);
		}
	}

	@Override
	public List<SysMenuEntity> queryMenuList(String domainId) {
		return sysMenuDao.queryMenuList(domainId);
	}

	@Override
	public List<SysMenuEntity> getUserMenuList(Map<String, Object> mapQuery) {
		List<SysMenuEntity> menuAllList=sysMenuDao.queryCurUserMenuList(mapQuery);
		return findMenuTreeChildList("0", menuAllList);
	}
	
	public List<SysMenuEntity> queryCurUserMenuList(Map<String, Object> mapQuery) {
		return sysMenuDao.queryCurUserMenuList(mapQuery);
	}

	protected List<SysMenuEntity> findMenuTreeChildList(String parentId,List<SysMenuEntity> menuAllList) {
		List<SysMenuEntity> menuUserList=new ArrayList<SysMenuEntity>();
		if(null!=menuAllList&&!menuAllList.isEmpty()){
			for (int i = 0; i < menuAllList.size(); i++) {
				if(parentId.equals(menuAllList.get(i).getParentId())){
					menuUserList.add(menuAllList.get(i));
				}
			}
			if(null!=menuUserList&&!menuUserList.isEmpty()){
				for (int i = 0; i < menuUserList.size(); i++) {
					if(hasMenuTreeChildList(menuAllList,menuUserList.get(i).getId())){
						menuUserList.get(i).setList(findMenuTreeChildList(menuUserList.get(i).getId(), menuAllList));
					}
				}
			}
		}
		return menuUserList;
	}

	private boolean hasMenuTreeChildList(List<SysMenuEntity> menuAllList,String parentId) {
		boolean result=false;
		if(null!=menuAllList&&!menuAllList.isEmpty()){
			for (int i = 0; i < menuAllList.size(); i++) {
				if(parentId.equals(menuAllList.get(i).getParentId())){
					result=true;break;
				}
			}
		}
		return result;
	}
}
